package portal.pubimpl.modules.pf;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import portal.pub.util.pf.BaseDao;
import portal.pub.util.pf.Constant;
import portal.pub.util.pf.JsonObjectUtil;
import portal.pub.util.pf.Result;
import portal.pubitf.modules.pf.IASService;
import portal.pubvo.modules.ASVo;

@Service("aSServiceImpl")
public class ASServiceImpl implements IASService {

	@Override
	public Result getAllByWhere(int page,int limit,String p_name,String c_name) {
		Result result = new Result();
		List<ASVo> vos = new ArrayList<ASVo>();
		Map<String,Object> asList = new HashMap<String,Object>();
		try {
			String where = "";
			if(p_name!=null&&!p_name.equals("")&&c_name!=null&&!c_name.equals("")) {
				where = " where project_name = '"+p_name+"' and contract_name like '\"+c_name+\"%' ";
			}
			if(c_name!=null&&!c_name.equals("")) {
				where = " where contract_name like '"+c_name+"%' ";
			}
			if(p_name!=null&&!p_name.equals("")) {
				where = " where project_name = '"+p_name+"' ";
			}
			
			String sql = "SELECT * FROM (SELECT ROWNUM AS rowno, t.* FROM (select * from bi_supplier_settle_push "+where+" order by project_start_date desc) t "
					+ "WHERE ROWNUM <= "+limit+"*"+page+" order by t.project_start_date desc) table_alias WHERE table_alias.rowno > "+limit+"*("+page+"-1)";
			ResultSet rs = new BaseDao(Constant.NC_DATASOURCE).exceuteQuery(sql);
			while(rs.next()){
				ASVo vo = new ASVo();
				vo.setContract_type(rs.getString("contract_type")==null?null:rs.getString("contract_type"));
				vo.setProject_name(rs.getString("project_name")==null?null:rs.getString("project_name"));
				vo.setContract_name(rs.getString("contract_name")==null?null:rs.getString("contract_name"));
				vo.setPk_contract(rs.getString("pk_contract")==null?null:rs.getString("pk_contract"));
				vo.setSupplier_name(rs.getString("supplier_name")==null?null:rs.getString("supplier_name"));
				vo.setPk_supplier(rs.getString("pk_supplier")==null?null:rs.getString("pk_supplier"));
				vo.setContract_money(rs.getString("contract_money")==null?null:rs.getString("contract_money"));
				vo.setChange_money(rs.getString("change_money")==null?null:rs.getString("change_money"));
				vo.setSettle_money(rs.getString("settle_money")==null?null:rs.getString("settle_money"));
				vo.setYf_money(rs.getString("yf_money")==null?null:rs.getString("yf_money"));
				vo.setFk_money(rs.getString("fk_money")==null?null:rs.getString("fk_money"));
				vo.setSupplier_code(rs.getString("supplier_code")==null?null:rs.getString("supplier_code"));
				vo.setProject_start_date(rs.getString("project_start_date")==null?null:rs.getString("project_start_date"));
				vo.setContract_date(rs.getString("contract_date")==null?null:rs.getString("contract_date"));
				vos.add(vo);
			}
			asList.put("code", 0);
			asList.put("msg", "");
			asList.put("count", this.getCount(where));
			asList.put("data", vos);
			
			result.setStatus("0");
			result.setJson(JsonObjectUtil.objectTojson(asList));
		} catch (Exception e) {
			result.setStatus("1");
		}
		return result;
	}

	@Override
	public int getCount(String where) {
		int count =0;
		try {
			String sql ="select count(*) count from bi_supplier_settle_push "+where+" ";
			ResultSet rs = new BaseDao(Constant.NC_DATASOURCE).exceuteQuery(sql);
			while(rs.next()){
				count = rs.getInt(1);
			}
		} catch (Exception e) {
			e.getStackTrace();
		}
		return count;
	}
}
